package cn.pzhuweb.gpa.utils;

import cn.pzhuweb.gpa.pojo.Gpa;
import cn.pzhuweb.gpa.pojo.Student;

import java.sql.*;
import java.util.List;

public class MySqlTool {

    Connection conn;

    {
        try {
            conn = JDBCUtils.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    Statement state = null;
    ResultSet rs = null;
    PreparedStatement psmt = null;

    public MySqlTool() throws SQLException {
    }

    public void insert(List<Student> students) throws SQLException {
        String sql = "insert into student (stuId,name,subject,credit,grade,time) VALUES (?,?,?,?,?,?)";
        PreparedStatement ps = null;
        ps = conn.prepareStatement(sql);
        for (Student student : students) {
            ps.setObject(1, student.getStuId());
            ps.setObject(2, student.getName());
            ps.setObject(3, student.getSubject());
            ps.setObject(4, student.getCredit());
            ps.setObject(5, student.getGrade());
            ps.setObject(6, student.getTime());
            ps.addBatch();
        }
        ps.executeBatch();
        ps.clearBatch();
    }

    public void insertGpa(Gpa gpa) throws SQLException {
        String sql = "insert into gpa (stuId,name,credit,time) VALUES (?,?,?,?)";
        PreparedStatement ps = null;
        ps = conn.prepareStatement(sql);
        ps.setObject(1, gpa.getStuId());
        ps.setObject(2, gpa.getName());
        ps.setObject(3, gpa.getCredit());
        ps.setObject(4, gpa.getTime());
        ps.addBatch();
        ps.executeBatch();
        ps.clearBatch();
    }

    public void close() {
        try {
            conn.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
}

